/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       TransPortData.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/8      feiwe                         add
 */


package com.fengwei.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TransPortData extends Thread {
    private final static Logger logger = LoggerFactory.getLogger(TransPortData.class);

    Socket getDataSocket;
    Socket putDataSocket;

    String type;

    public TransPortData(Socket getDataSocket, Socket putDataSocket, String type) {
        this.getDataSocket = getDataSocket;
        this.putDataSocket = putDataSocket;
        this.type = type;
    }

    @Override
    public void run() {
        try {
            while (true) {

                InputStream in = getDataSocket.getInputStream();
                OutputStream out = putDataSocket.getOutputStream();
                //读入数据
                byte[] data = new byte[1024];
                int readlen = in.read(data);

                //如果没有数据，则暂停
                if (readlen <= 0) {
                    Thread.sleep(300);
                    continue;
                }


                out.write(data, 0, readlen);
                out.flush();
            }
        } catch (Exception e) {
            logger.error("type:" + type, e);
        } finally {
            //关闭socket
            try {
                if (putDataSocket != null) {
                    putDataSocket.close();
                }
            } catch (Exception exx) {
            }

            try {
                if (getDataSocket != null) {
                    getDataSocket.close();
                }
            } catch (Exception exx) {
            }
        }
    }

    public void destroy() {
        //关闭socket
        try {
            if (putDataSocket != null) {
                putDataSocket.close();
            }
        } catch (Exception exx) {
        }

        try {
            if (getDataSocket != null) {
                getDataSocket.close();
            }
        } catch (Exception exx) {
        }
    }
}
